$(function(){
	$('#notice-create-msg').hide();
	$("#form-add").validate({
		rules: {
			"name": "required",
			"linkUrl":"url"
		},
		messages:{
			"name": {
				required: "请输入标题"
			}
		},
		showErrors: function(errorMap, errorList){
						if(errorList.length > 0) {
							var error = errorList[0];
							$(error.element).parent().addClass('has-error');
							$('#notice-create-msg').show().find('p').text(error.message);
						}
		},
		submitHandler: function(form) 
		   {      
				add();
		   } 
	});
	$("#form-edit").validate({
		rules: {
			"name": "required"
		},
		messages:{
			"name": {
				required: "请输入标题"
			}
		},
		showErrors: function(errorMap, errorList){
						if(errorList.length > 0) {
							var error = errorList[0];
							$(error.element).parent().addClass('has-error');
							$('#notice-create-msg').show().find('p').text(error.message);
						}
		},
		submitHandler: function(form) 
		   {      
				edit();
		   } 
	});
	 function add(){
		var data = {};
		data['name'] = $("#add-modal").find("input[name='name']").val();
		data['linkUrl'] = $("#add-modal").find("input[name='linkUrl']").val();
		cAjax({
			url: '/web/home/addFriendLink',
			data:data,
			success: function(obj) {
				greeting({
					msg: '添加成功！'
				});
				window.location.reload();
		    }
		});
	}
	
	$(".btn-edit").click(function(){
		var top = $(this).closest("tr");
		var id = $(top).find("td:eq(1)").html();
		var name = $(top).find("td:eq(2)").html();
		var linkUrl = $(top).find("td:eq(3)").html();
		$("#edit-modal").find("input[name='id']").val(id);
		$("#edit-modal").find("input[name='name']").val(name);
		$("#edit-modal").find("input[name='linkUrl']").val(linkUrl);
		$('#edit-modal').modal('show');
	})
	
	function edit(){
		var data = {};
		data['id'] = $("#edit-modal").find("input[name='id']").val();
		data['name'] = $("#edit-modal").find("input[name='name']").val();
		data['linkUrl'] = $("#edit-modal").find("input[name='linkUrl']").val();
		cAjax({
			url: '/web/home/editFriendLink',
			data:data,
			success: function(obj) {
				greeting({
					msg: '编辑成功！'
				});
				window.location.reload();
		    }
		});
	}
	
	$(".btn-del").click(function(){
		var cur = $(this);
		var id = $(cur).closest('tr').attr('data-id');
		actionConfirm({
			msg: '确定删除吗？',
			confirm: function() {
				cAjax({
					url: '/web/home/delFriendLink',
					data: {
						id: id,
					},
					success: function(){
						greeting({
							msg: '删除成功！'
						});
						$(cur).closest('tr').fadeOut();
					}
				});
			}
		});
	})
	
	/*
	 * 排序保存
	 */
	var $sortList = $('.sortable-list');
	$sortList.sortable({
		scroll: true,
		opacity: 0.6,
		cursor: "move",
		axis:"y",
		handle: '.dragbar',
		scrollSensitivity: 80,
		stop: function(evt,ui) {
		}
	});

	function saveOrder() {
		var orderList = [];
		$('.sortable-list>tr').each(function(index){
			var $this = $(this);
			orderList.push({
				id: $this.attr('data-id'),
				sort: index+1
			});	
		});		
		cAjax({
			url: '/web/home/sortFriendLink',
			data: {
				sortData: JSON.stringify(orderList)
			},
			success: function() {
				greeting({
					msg: '导航顺序保存成功'
				});
			}
		});
	}		

	$('#save-btn').on('click',function(){
		saveOrder();
	});

})
